package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckGroupService;
import org.apache.xmlbeans.impl.jam.mutable.MClass;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名称: CheckGroupController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/11 上午 11:23
 * Version 1.0.0
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /*普通查询*/
    @GetMapping("/findAll")
    public Result findAll(){
       List<CheckGroup> list= checkGroupService.findAll();
       return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /*分页查询*/
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
     PageInfo<CheckGroup> pageInfo= checkGroupService.findPage(queryPageBean);
     return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,new PageResult(pageInfo.getTotal(),pageInfo.getList()));
    }
    /*添加*/
    @PostMapping("/add")
    public Result add(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        checkGroupService.addCheckGroupAndItem(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /*编辑：先根据id查询 然后再进行修改*/
    @GetMapping("/findById")
    public Result findById(Integer id){
      CheckGroup checkGroup=  checkGroupService.findById(id);
      return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }
    /*进行修改*/
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        checkGroupService.update(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /*删除 这个操作比较危险*/
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        checkGroupService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @GetMapping("findByCheckItemId")
    public Result findByCheckItemId(Integer id){
      List<Integer> integers =  checkGroupService.findByCheckItemId(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,integers);
    }

}
